import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import plugins.OuterIF;

public class GetPlugIns {
	public GetPlugIns(HashMap<String, OuterIF> pluginMap, ArrayList<String> pluginNames) {

		String cpath = System.getProperty("user.dir") + File.separator + "plugins";
		try {
			File f = new File(cpath);
			String[] files = f.list();
			for (int i = 0; i < files.length; i++) {
				if (files[i].endsWith(".jar")) {
					File file = new File(cpath + File.separator + files[i]);
					JarFile jar = new JarFile(file);
					Manifest mf = jar.getManifest();
					Attributes att = mf.getMainAttributes();
					String cname = att.getValue("Plugin-Class");
					URL url = file.getCanonicalFile().toURI().toURL();
					URLClassLoader loader = new URLClassLoader(new URL[] { url });
					Class<?> cobj = loader.loadClass(cname);
					Class[] ifnames = cobj.getInterfaces();
					for (int j = 0; j < ifnames.length; j++) {
						if (ifnames[j] == OuterIF.class) {
							OuterIF plugin = (OuterIF) cobj.newInstance();
							pluginMap.put(cname, plugin);
							pluginNames.add(cname);
							break;
						}
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
